package com.smedi.ismedi.inmail.core.domain.service.impl;

import com.smedi.ismedi.authority.starter.annotation.SkipAuthority;
import com.smedi.ismedi.database.starter.annotation.SkipSqlPermission;
import com.smedi.ismedi.inmail.core.application.command.TaskRecordCommand;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.Criteria;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.TaskRecord;
import com.smedi.ismedi.inmail.core.domain.repository.TaskRecordRepository;
import com.smedi.ismedi.inmail.core.domain.service.TaskDomainService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class TaskDomainServiceImpl implements TaskDomainService {
    private final TaskRecordRepository taskRecordRepository;

    @Override
    public Boolean updateTaskRecord(TaskRecord taskRecord) {
        taskRecord.setTaskTime(LocalDateTime.now());
        if ("已完成,已关闭,已删除,已撤销".contains(taskRecord.getTaskStatus())) {
            taskRecord.setFinishTime(LocalDateTime.now());
        }
        return taskRecordRepository.doUpdateStatusByOldStatus(taskRecord);
    }

    @Override
    public TaskRecord addTaskRecord(TaskRecord taskRecord) {
        taskRecord.setTaskTime(LocalDateTime.now());
        return taskRecordRepository.doInsert(taskRecord);
    }

    @Override
    public TaskRecord editTaskRecord(TaskRecord taskRecord) {
        taskRecord.setTaskTime(LocalDateTime.now());
        return taskRecordRepository.doUpdate(taskRecord);
    }

    @Override
    public Boolean deleteTaskList(List<Long> ids) {
        return taskRecordRepository.doDeleteList(ids);
    }

    @Override
    public Boolean deleteTaskListBySourceIds(List<TaskRecord> taskRecords) {
        return taskRecordRepository.doDeleteBySourceIds(taskRecords);
    }

    @Override
    public List<TaskRecord> readTaskList(List<Long> ids) {
        List<TaskRecord> taskRecords = taskRecordRepository.doList(ids);
        for (TaskRecord taskRecord : taskRecords) {
            taskRecord.setTaskRead(true);
            taskRecord.setTaskTime(LocalDateTime.now());
            if ("日志审阅".equals(taskRecord.getTaskName())) {
                taskRecord.setTaskStatus("已完成");
                taskRecord.setFinishTime(LocalDateTime.now());
            }
        }
        return taskRecordRepository.doUpdateList(taskRecords);
    }

    @Override
    @SkipAuthority
    @SkipSqlPermission
    public TaskRecord readTaskDaily(TaskRecord taskRecord) {
        return taskRecordRepository.doUpdateTaskDaily(taskRecord);
    }

    @Override
    @SkipAuthority
    @SkipSqlPermission
    public List<TaskRecord> queryTaskByTypeAndStatus(String taskName, String status) {
        return taskRecordRepository.doQueryTaskByTypeAndStatus(taskName, status);
    }

    @Override
    @SkipAuthority
    @SkipSqlPermission
    public List<TaskRecord> queryTaskByBySourceFromIdAndOldStatus(Long sourceFromId, String oldStatus) {
        return taskRecordRepository.doListBySourceFromIdAndOldStatus(sourceFromId, oldStatus);
    }

    @Override
    public boolean updateTaskStatusRegularly() {
        Criteria criteria = new Criteria();
        List<String> taskTypes = new ArrayList<>();

        taskTypes.add("安全日志");
        taskTypes.add("每日晨会");
        criteria.setTaskTypes(taskTypes);
        // 即将开始的安全日志和
        taskRecordRepository.doQueryTask(criteria);
        return false;
    }

    @Override
    public boolean updateTaskStatusWeekly() {
        return false;
    }

    @Override
    public boolean updateTaskStatusMonthly() {
        return false;
    }

    @Override
    public boolean deleteHistoryTask() {
        return taskRecordRepository.deleteHistoryTask();
    }

    @Override
    public Boolean cancelTask(TaskRecord taskRecord) {
        return taskRecordRepository.cancelTask(taskRecord);
    }
}
